<?php

session_set_cookie_params(0, NULL, NULL, NULL, TRUE); //http only cookie
session_start();

if(!isset($_SESSION['isLogged']) || $_SESSION['isLogged'] != true) header('Location: index.php');

require 'includes/common.php';

$selectedItem = (int)$_GET['item'];

if ( !isset($selectedItem) || !is_numeric($selectedItem) || $selectedItem < 0 ) {
    header('Location:admin.php');
    exit();
}


$pageTitle="File Upload System: ADD";
include "includes/header_admin.php";


//CHECK MALE OR FEMALE ITEM
if ((int)$selectedItem > 299) {
    $arrayHolder = $pics_women;
    $path_all = str_replace("../","",$path_txt_w.$pics_women[$selectedItem]['en_name'].'.txt');
} else {
    $arrayHolder = $pics_men;
    $path_all = str_replace("../","",$path_txt.$pics_men[$selectedItem]['en_name'].'.txt');
}

//echo '<br/>'.$path_all;


//GET NEW ITEM VALUES IN ARRAY AND WRITE TO FILE
if(isset($_POST['edit'])) {

    if(file_exists($path_all)) {
        $result_edit = file($path_all);
    } else {
        die ('Несъществуващ файл');
    }

    $result_edit[0] = htmlspecialchars(addslashes($_POST['bg_name'] ));
    $result_edit[1] = strip_tags($_POST['descr'] );
    $result_edit[2] = htmlspecialchars(floatval($_POST['price'] ));
    $result_edit[3] = strip_tags($_POST['sizes'] );

    //DATA INPUT NORMALIZATION? :)
    $result_new = implode("\r\n",$result_edit);
    $result_new = preg_replace('~\r[\n]?~', "\n", $result_new);

    if(!file_put_contents($path_all, $result_new)) {
        echo 'Грешка при записването. <a href="admin.php">Върни се обратно.</a>';
        $error_add = true;
    }
}

//echo "<pre>".print_r($result_edit, true)."</pre>";
//echo "<pre>".print_r($result_new, true)."</pre>";

?>

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
    <title></title>

</head>
<body>
<?php if(isset($_POST['edit']) && !isset($error_add)) : ?>
    <br/><p>Успешно редактирахте записа</p>
    <a href="admin.php">Върни се обратно.</a>
<?php endif; ?>

<?php if (!isset($_POST['edit']) ) : ?>
    <br/><br/><a href="admin.php">Върни се обратно.</a><hr><br/>
    <div>
        <h3>Система за промяна на данните за съществуващи тениски: </h3><br/>
        <form method="POST" enctype="multipart/form-data" style="width: 700px; display:inline-block;">
            Име на тениската:<input type="text" name="bg_name" size="60"
                                    value="<?php echo $arrayHolder[$selectedItem]['bg_name'];?>"/><br/>
            Цена:<input type="text" name="price" value="<?php echo $arrayHolder[$selectedItem]['price'];?>"/><br/>
            Размери:<input type="text" name="sizes" value="<?php echo $arrayHolder[$selectedItem]['sizes'];?>"/><br/>
            Описание:<textarea rows="10" cols="40" name="descr"><?php echo $arrayHolder[$selectedItem]['descr'];?></textarea>
            <input type="submit" name="edit" value="Запиши промените"/>
        </form>
        <img style="display:inline-block;" src="<?php echo $arrayHolder[$selectedItem]['loc_small'];?>"/>
    </div>
<?php endif; ?>


</body>
</html>